package com.prs.crm.action;

import java.io.FileInputStream;
import java.io.InputStream;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.XJAction;

import com.prs.crm.context.Constrains;
import com.prs.crm.domain.Attachment;
import com.prs.crm.service.AttachmentService;

public class AttachmentAction extends XJAction{


	private static final long serialVersionUID = 2378132394752043361L;
	
	private AttachmentService attachmentService;
	
	private Attachment attach;
	
	private InputStream inputstream;
	
	private String fileName;

	public void setAttachmentService(AttachmentService attachmentService) {
		this.attachmentService = attachmentService;
	}

	public AttachmentService getAttachmentService() {
		return attachmentService;
	}

	public void setAttach(Attachment attach) {
		this.attach = attach;
	}

	public Attachment getAttach() {
		return attach;
	}
	
	public void setInputstream(InputStream inputstream) {
		this.inputstream = inputstream;
	}

	public InputStream getInputstream() {
		return inputstream;
	}
	
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}

	public String getFileName() {
		return fileName;
	}

	@Action(value="download", results = { @Result(name = "success"
		,type="stream",params={"contentType","application/octet-stream","inputName","inputstream"
		,"bufferSize","1024","contentDisposition","filename=${fileName}"}) })
	public String download() throws Exception
	{
		fileName = new String(attach.getName().getBytes(),"ISO-8859-1");
		if (attach.getType()!=null && attach.getType()==Constrains.ATTACHMENTTYPE_DISK)
		{
			this.setInputstream(new FileInputStream(attach.getPath()));
		}
		else
		{
			this.setInputstream(ServletActionContext.getServletContext().getResourceAsStream(attach.getPath()));
		}
		return SUCCESS;
	}


	
	

}
